﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for StringUtil
/// </summary>
public class StringUtil
{
    public static string TruncateString(string item, int maxChars)
    {
        String r = String.Empty;
        if (!String.IsNullOrEmpty(item) && item.Length >= maxChars)
        {
            r = item.Substring(0, maxChars);
            for (int i = r.Length - 1; i > 0; i--)
            {
                if (r[i] == ' ')
                {
                    break;

                }
                else
                {
                    r = r.Remove(i);
                }
            }

            return r + "...";
        }
        return item;
    }
    public static String PadLeft(String str, int len, char pad)
    {
        if (str.Length >= len)
            return str;
        int max = len - str.Length;
        char[] chrs = new char[max];
        for (int i = 0; i < max; i++)
            chrs[i] = pad;
        return (new String(chrs)) + str;
    }

    public static bool CheckPassWordHaveNumberAndChar(String str)
    {
        bool isNumber = false;
        bool isChar = false;
        if (str == null || str.Length == 0)
            return false;
        foreach (char chr in str)
        {
            if (char.IsDigit(chr))
            {
                isNumber = true;
                break;
            }
        }

        foreach (char chr in str)
        {
            if (!char.IsDigit(chr))
            {
                isChar = true;
                break;
            }
        }
        if (isNumber && isChar)
        {
            return true;
        }
        return false;
    }

    public static string NumberToWords2(Double num)
    {

        string s = NumberToWords(num);
        return UppercaseFirst(s) + " đồng";
    }
    public static string NumberToWords(Double num)
    {
        Int64 number = Convert.ToInt64(num);
        if (number == 0)
            return "không";

        if (number < 0)
            return "âm " + NumberToWords(Math.Abs(number));

        string words = "";

        if ((number / 1000000000) > 0)
        {
            words += NumberToWords(number / 1000000000) + " tỉ ";
            number %= 1000000000;
        }

        if ((number / 1000000) > 0)
        {
            words += NumberToWords(number / 1000000) + " triệu ";
            number %= 1000000;
        }

        if ((number / 1000) > 0)
        {
            words += NumberToWords(number / 1000) + " ngàn ";
            number %= 1000;
        }

        if ((number / 100) > 0)
        {
            words += NumberToWords(number / 100) + " trăm ";
            number %= 100;
        }

        if (number > 0)
        {
            var unitsMap = new[] { "không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám",
                                       "chín", "mười", "mười một", "mười hai", "mười ba", "mười bốn", "mười lăm",
                                       "mười sáu", "mười bảy", "mười tám", "mười chín" };
            var tensMap = new[] { "zero", "mười", "hai mươi", "ba mươi", "bốn mươi",
                                      "năm mươi", "sáu mươi", "bảy mươi", "tám mươi", "chín mươi" };

            if (number < 20)
                words += unitsMap[number];
            else
            {
                words += tensMap[number / 10];
                if ((number % 10) > 0)
                    words += " " + unitsMap[number % 10];
            }
        }

        return words;
    }
    public static string UppercaseFirst(string s)
    {
        if (string.IsNullOrEmpty(s))
        {
            return string.Empty;
        }
        return char.ToUpper(s[0]) + s.Substring(1);
    }
}